package br.com.wuo.karina.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import br.com.wuo.karina.model.Turma;
import br.com.wuo.karina.model.dao.impl.TurmaDaoImpl;

import net.miginfocom.swing.MigLayout;

public class SalvarTurma extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtNome;
	private MigLayout layout;
	private JButton btnSave, btnClear, btnExit;
	private TurmaDaoImpl turmaDao;
	
	public SalvarTurma(){
		super ("Salvar Turma");
		initialize();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        addListener();
	}
	
	
	public void initialize() {
        layout = new MigLayout("", "60px[100][100][100]80px", "");
        setLayout(layout);
                
        turmaDao = new TurmaDaoImpl();
        
        txtNome = new JTextField();
        
        btnSave = new JButton("Salvar");
        btnClear = new JButton("Limpar");
        btnExit = new JButton("Sair");
        
        add(new JLabel("Nome Turma: "));
        add(txtNome, "w 230!, wrap");
        
        add(btnSave, "split 3, span 2, align 50% 50%");
        add(btnClear);
        add(btnExit);
        pack();
	
	}
	
	private void addListener() {
		btnSave.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					turmaDao.SalvarTurma(new Turma(txtNome.getText()));
					txtNome.setText("");
				} catch (SQLException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "Turma não salva");
				}
				
			}
		});
		
	};
	
}
